package DynamicProgram.completeBag;

/**
 * @ClassName CombationIV
 * @Description TODO
 * @Author lenovo
 * @Date 2023-08-21 10:05
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class CombationIV {

    /**
     * 难度：中等
     * <p>
     * 给定一个由正整数组成且不存在重复数字的数组，找出和为给定目标正整数的组合的个数。
     * <p>
     * 示例:
     * <p>
     * nums = [1, 2, 3]
     * target = 4
     * 所有可能的组合为： (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1)
     * <p>
     * 请注意，顺序不同的序列被视作不同的组合。
     * <p>
     * 因此输出为 7。
     */

    public int combinationSum4(int[] nums, int target) {
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for (int i = 0; i <= target; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i >= nums[j]) {
                    dp[i] += dp[i - nums[j]];
                }
            }
        }
        for (int i : dp) {
            System.out.println(i);
        }
        return dp[target];
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3};
        int target = 3;
        CombationIV combationIV = new CombationIV();
        combationIV.combinationSum4(nums, target);
    }
}